﻿using System;
using System.Collections.Generic;
using System.IO;
using Abp.Collections.Extensions;
using Abp.Dependency;
using OfficeOpenXml;
using Project.Net.MimeTypes;
using Abp.Application.Services;
using Project.Apps.DataExporting.Dto;

namespace TransportationMonitor.DataExporting.Excel.EpPlus
{
    public abstract class EpPlusExcelExporterBase : ApplicationService, ITransientDependency
    {
        //public IAppFolders AppFolders { get; set; }

        protected FileDto CreateExcelPackage(string fileName, Action<ExcelPackage> creator)
        {
            var file = new FileDto(fileName, MimeTypeNames.ApplicationVndOpenxmlformatsOfficedocumentSpreadsheetmlSheet);

            using (var excelPackage = new ExcelPackage())
            {
                creator(excelPackage);
                Save(excelPackage, file);
            }

            return file;
        }

        protected void AddHeader(ExcelWorksheet sheet, params string[] headerTexts)
        {
            if (headerTexts.IsNullOrEmpty())
            {
                return;
            }

            for (var i = 0; i < headerTexts.Length; i++)
            {
                AddHeader(sheet, i + 1, headerTexts[i]);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sheet"></param>
        /// <param name="headerIndex">表头索引,默认1</param>
        /// <param name="headerTexts"></param>
        protected void AddHeader(ExcelWorksheet sheet, int headerIndex = 1, bool hasBorder = false, params string[] headerTexts)
        {
            if (headerTexts.IsNullOrEmpty())
            {
                return;
            }

            for (var i = 0; i < headerTexts.Length; i++)
            {
                AddHeader(sheet, i + 1, headerTexts[i], headerIndex, hasBorder);
            }
        }

        protected void AddHeader(ExcelWorksheet sheet, int columnIndex, string headerText)
        {
            sheet.Cells[1, columnIndex].Value = headerText;
            sheet.Cells[1, columnIndex].Style.Font.Bold = true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sheet"></param>
        /// <param name="columnIndex"></param>
        /// <param name="headerText"></param>
        /// <param name="headerIndex">表头索引，默认1</param>
        protected void AddHeader(ExcelWorksheet sheet, int columnIndex, string headerText, int headerIndex = 1, bool hasBorder = false)
        {
            sheet.Cells[headerIndex, columnIndex].Value = headerText;
            sheet.Cells[headerIndex, columnIndex].Style.Font.Bold = true;
            if (hasBorder)
            {
                sheet.Cells[headerIndex, columnIndex].Style.Border.BorderAround(OfficeOpenXml.Style.ExcelBorderStyle.Thin);
            }
        }

        protected void AddObjects<T>(ExcelWorksheet sheet, int startRowIndex, IList<T> items, params Func<T, object>[] propertySelectors)
        {
            if (items.IsNullOrEmpty() || propertySelectors.IsNullOrEmpty())
            {
                return;
            }

            for (var i = 0; i < items.Count; i++)
            {
                for (var j = 0; j < propertySelectors.Length; j++)
                {
                    sheet.Cells[i + startRowIndex, j + 1].Value = propertySelectors[j](items[i]);
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sheet"></param>
        /// <param name="startRowIndex"></param>
        /// <param name="items"></param>
        /// <param name="hasBorder"></param>
        /// <param name="propertySelectors"></param>
        protected void AddObjects<T>(ExcelWorksheet sheet, int startRowIndex, IList<T> items, bool hasCellBorder, params Func<T, object>[] propertySelectors)
        {
            if (items.IsNullOrEmpty() || propertySelectors.IsNullOrEmpty())
            {
                return;
            }

            for (var i = 0; i < items.Count; i++)
            {
                for (var j = 0; j < propertySelectors.Length; j++)
                {
                    sheet.Cells[i + startRowIndex, j + 1].Value = propertySelectors[j](items[i]);
                    if (hasCellBorder)
                    {
                        sheet.Cells[i + startRowIndex, j + 1].Style.Border.BorderAround(OfficeOpenXml.Style.ExcelBorderStyle.Thin);
                    }
                }
            }
        }

        protected void Save(ExcelPackage excelPackage, FileDto file)
        {
            var filePath = Path.Combine("D:\\Downloads\\", file.FileName);
            excelPackage.SaveAs(new FileInfo(filePath));
        }


    }

    public enum ExcelCellStyle
    {

    }
}